/**
 * @author :    hippo
 * @date :      2023-07-30
 * @copyright   Apache 2.0
*/

#ifndef _SQLCONNPOOL_H
#define _SQLCONNPOOL_H

#include <mysql/mysql.h>
#include <string>
#include <queue>
#include <mutex>
#include <semaphore.h>
#include <thread>
//#include "log.h"

class SqlConnPool {
private:
    int MAX_CONN_;
    int use_count_;
    int free_count_;
    
    std::queue<MYSQL *> conn_que_;
    std::mutex mutex_;
    sem_t sem_id_;

public:
    static SqlConnPool *Instance();

    MYSQL *GetConn();
    void FreeeConn(MYSQL *conn);
    int GerFreeConnCount();

    void Init(const char *host, int port,
              const char *user, const char *pwd,
              const char *dv_name, int conn_size);
    void ClosePool();

private:
    SqlConnPool();
    ~SqlConnPool();
};

#endif // _SQLCONNPOOL_H